<script setup>
import ImageUploader from '@/components/admin/ImageUploader.vue'
import errorLogger from '@/utils/errorLogger'
import { reactive } from 'vue'
import { useAdminStore } from '@/stores'

const adminStore = useAdminStore()

const adminInfoForm = reactive({
  id: adminStore.adminInfo._id,
  name: adminStore.adminInfo.name,
  email: adminStore.adminInfo.email,
  avatar: adminStore.adminInfo.avatar,
  github: adminStore.adminInfo.github,
  gitee: adminStore.adminInfo.gitee,
  juejin: adminStore.adminInfo.juejin,
  description: adminStore.adminInfo.description,
})

// 更新管理员信息
const updateAdminInfo = async () => {
  try {
    await adminStore.updateAdminInfo(adminInfoForm)
  } catch (error) {
    errorLogger(error)
  }
}

const passwordForm = reactive({
  id: adminStore.adminInfo._id,
  newPassword: '',
  confirmPassword: '',
})

// 更新管理员密码
const updateAdminPassword = async () => {
  try {
    await adminStore.updateAdminPassword(passwordForm)
  } catch (error) {
    errorLogger(error)
  }
}
</script>

<template>
  <el-card>
    <template #header>
      <el-text size="large">账户管理</el-text>
    </template>

    <el-row :gutter="50">
      <!-- 更新管理员信息 -->
      <el-col :span="12">
        <el-form label-width="auto" :model="adminInfoForm">
          <el-form-item class="customize-item">
            <ImageUploader v-model="adminInfoForm.avatar" />
            <el-text>点击选择选择头像</el-text>
          </el-form-item>
          <el-form-item label="昵称">
            <el-input v-model="adminInfoForm.name" placeholder="请输入昵称" />
          </el-form-item>
          <el-form-item label="邮箱">
            <el-input v-model="adminInfoForm.email" placeholder="请输入邮箱" />
          </el-form-item>
          <el-form-item label="GitHub">
            <el-input
              v-model="adminInfoForm.github"
              placeholder="请输入GitHub链接" />
          </el-form-item>
          <el-form-item label="Gitee">
            <el-input
              v-model="adminInfoForm.gitee"
              placeholder="请输入Gitee链接" />
          </el-form-item>
          <el-form-item label="掘金">
            <el-input
              v-model="adminInfoForm.juejin"
              placeholder="请输入掘金链接" />
          </el-form-item>
          <el-form-item label="个人简介">
            <el-input
              v-model="adminInfoForm.description"
              placeholder="请输入个人简介" />
          </el-form-item>
          <el-form-item class="customize-item">
            <el-button
              @click="updateAdminInfo"
              class="submit-btn"
              type="primary"
              >保存</el-button
            >
          </el-form-item>
        </el-form>
      </el-col>

      <!-- 更新管理员密码 -->
      <el-col :span="12">
        <el-form label-width="auto" :model="passwordForm">
          <el-form-item label="新密码">
            <el-input
              v-model="passwordForm.newPassword"
              type="password"
              placeholder="请输入新密码"
              show-password />
          </el-form-item>
          <el-form-item label="确认新密码">
            <el-input
              v-model="passwordForm.confirmPassword"
              type="password"
              placeholder="请再次输入新密码"
              show-password />
          </el-form-item>
          <el-form-item class="customize-item">
            <el-button
              @click="updateAdminPassword"
              class="submit-btn"
              type="primary"
              >更新密码</el-button
            >
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </el-card>
</template>

<style scoped>
:deep(.customize-item) .el-form-item__content {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;

  .submit-btn {
    width: 100px;
  }
}
</style>
